package cn.sciento.wechat.enterprise.service.impl;

import javax.annotation.Resource;
import org.apache.commons.codec.digest.DigestUtils;
import cn.sciento.wechat.enterprise.dto.GetTicketResultDTO;
import cn.sciento.wechat.enterprise.service.WechatTicketService;
import org.springframework.web.client.RestTemplate;

public class WechatTicketServiceImp implements WechatTicketService {
  @Resource(name = "wdRestTemplate")
  protected RestTemplate restTemplate;
  
  public GetTicketResultDTO getTicket(String accessToken, String agent_config) {
    return (GetTicketResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=" + accessToken + "&type" + agent_config, GetTicketResultDTO.class);
  }
  
  public GetTicketResultDTO getEnterpriseTicket(String accessToken) {
    return (GetTicketResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" + accessToken, GetTicketResultDTO.class);
  }
  
  public String algorithmSignature(String noncestr, String jsapi_ticket, String timestamp, String url) {
    String string = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
    return DigestUtils.sha1Hex(string);
  }
}
